<?php
declare (strict_types=1);

namespace app\common\model\payment;

use app\common\model\BaseModel;
use app\promotion\model\Promotion;
use app\user\model\user\Account;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;
use think\model\relation\HasOneThrough;

/**
 * 订单模型
 */
class PaymentOrder extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'payment_order';

    protected $pk = 'order_no';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    protected $hidden = ['delete_time'];

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'addrinfo'    => 'json',
        'create_time' => 'integer',
        'update_time' => 'integer',
        'close_time'  => 'integer',
        'delete_time' => 'integer',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id')->append(['avatar_url']);
    }

    public function scopeUserOrder($query)
    {
        $query->where('user_id', request()->mid);
    }

    public function scopeFinish($query)
    {
        $query->where('status', 'IN', '20,30,40,50');
    }

    public function scopeWaiting($query)
    {
        $query->where('status', 10);
    }

    public function scopeCancel($query)
    {
        $query->where('status', 0);
    }

    public function scopeDate($query, $date = '')
    {
        switch ($date) {
            case 'this_year':
                $query->where('update_time', '>', strtotime(date('Y', time()) . '-1-1'));
                break;

            case 'earlier':
                $query->where('update_time', '<', strtotime(date('Y', time()) . '-1-1'));
                break;

            default:
                if ($date) {
                    $query->where('update_time', '>', strtotime(date('Y-m-d', strtotime('-' . $date))));
                }
                break;
        }
    }

    public function itemOne(): HasOne
    {
        return $this->hasOne(PaymentOrderItem::class, 'order_no', 'order_no');
    }

    public function itemMany(): HasMany
    {
        return $this->hasMany(PaymentOrderItem::class, 'order_no', 'order_no');
    }

    /**
     * 积分抵扣订单
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-08-26
     *
     */
    public function redeemCreditSub(): HasMany
    {
        return $this->hasMany(PaymentOrderItem::class, 'order_no', 'order_no')
            ->where('credit', '<>', '')
            ->where('credit->credit', '<>', '');
    }

    public function getStatusText(): array
    {
        return $this->getStatusTextInfo();
    }

    protected function getStatusTextInfo(): array
    {
        return [
            0  => '已取消',
            10 => '待支付',
            20 => '已支付',
            30 => '已申请退款,待确认',
            40 => '退款已确认,退款中',
            50 => '已退款',
            60 => '已失效',
            70 => '已支付,但被管理员移除',
            80 => '待成团/已支付',
        ];
    }

    public function promotion(): HasOneThrough
    {
		return $this->hasOneThrough(Promotion::class, PaymentOrderItem::class, 'order_no', 'id', 'order_no', 'promotion_id')->append(['type_text']);
	}

    public function payway(): HasOne
    {
        return $this->hasOne(Transaction::class, 'order_no', 'order_no')->bind(['payway' => 'payment_type']);
    }

}
